package cn.ultrarisk.scadmin.manager.biz.solr.dao;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.solr.context.SolrState;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrShard;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * solr集合分片数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class SolrShardDAO extends BaseDAO<SolrShard, Integer> {
    /**
     * 构造器
     */
    public SolrShardDAO() {
        super(SolrShard.class, SolrShard.COLUMN_PROPERTY_MAP);
    }

    public SolrShard create(String shardName, Integer collectionId) {
        SolrShard shard = new SolrShard();
        shard.setName(shardName);
        shard.setCollectionId(collectionId);
        shard.setState(SolrState.RECOVERING);
        shard.setAvailable(true);
        shard.setUpdateTime(System.currentTimeMillis());
        create(shard);
        return shard;
    }

    public List<SolrShard> findAvailable(Integer collectionId) {
        Query query = Query.query(SolrShard.AVAILABLE, true).eq(SolrShard.COLLECTION_ID, collectionId);
        return findList(query);
    }

    public SolrShard getByName(String name, Integer collectionId) {
        Query query = Query.query(SolrShard.COLLECTION_ID, collectionId).eq(SolrShard.NAME, name);
        return get(query);
    }
}
